﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualDisk
{
    class RmdirCommand:Command
    {
        private MyString[] arguments;
       
        protected override void processError(int returnCode)
        {
            switch (returnCode)
            {
                case 1:
                    Console.WriteLine("命令语法不正确！");
                    break;
                case 2:
                    Console.WriteLine("系统找不到指定的文件！");
                    break;
                case 3:
                    Console.WriteLine("无效参数！");
                    break;
                case 4:
                    Console.WriteLine("目录名无效！");
                    break;
                case 5:
                    Console.WriteLine("另一个程序正在使用此文件，进程无法访问!");
                    break;
                default:
                    break;
            }
        }

        private void processArgument(out MyString path, out MyString param)
        {
            path = MyString.Empty;
            param = MyString.Empty;
            if (arguments.Length == 1)
            {
                path = arguments[0].Trim();
            }
            else if (arguments.Length == 2 && arguments[0][0] == '/')
            {
                param = arguments[0].Trim();
                path = arguments[1].Trim();
            }
            else
            {
                path = (arguments[0] + arguments[1]).Trim();
            }
        }

        protected override int run()
        {
            MyString param;
            MyString path;
            if (arguments.Length == 0 || arguments == null)
            {
                return 1;
            }

            processArgument(out path, out param);
            //参数是否有效
            if (!MyString.IsNullOrEmpty(param) && param != new MyString("/s"))
            {
                return 3;
            }
            //路径是否存在
            if (!DiskTree.getInstance().isExist(path))
            {
                return 2;
            }

            //是否是目录
            if (!DiskTree.getInstance().isADirctory(path))
            {
                return 4;
            }
            MyString pwd = DiskTree.getInstance().getPWDPath();
            if (DiskTree.getInstance().HasRelationship(path,pwd))
            {
                return 5;
            }
            //目录为空直接删除
            if (DiskTree.getInstance().isEmptyDirctory(path))
            {
                DiskTree.getInstance().removeNode(path);
            }
            else
            {
                if (param == new MyString("/s"))
                {
                    MyString strtemp = null;
                    while (true)
                    {
                        Console.Write(path + ", 是否确认(Y/N)? ");
                        strtemp = new MyString(Console.ReadLine());
                        if (strtemp.ToLower() == new MyString("y"))
                        {
                            DiskTree.getInstance().removeNode(path);
                            break;
                        }
                        else if (strtemp.ToLower() == new MyString("n"))
                        {
                            break;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("目录不为空！");
                }

            }
            return 0;
        }
        public RmdirCommand(MyString[] argu)
        {
            arguments = new MyString[argu.Length];
            for (int i = 0; i < arguments.Length; i++)
            {
                arguments[i] = argu[i];
            }
           
        }
    }
}
